﻿#if (!SILVERLIGHT)

using System;
using System.Reactive.Disposables;
using System.Threading.Tasks;
using nRoute.Components.Composition;

namespace nRoute.ApplicationServices
{
    public partial class nRouteApplicationService
    {
        private readonly WriteOnce<bool> _autoInitialize = new WriteOnce<bool>();

#region Properties

        public bool AutoInitialize
        {
            get { return _autoInitialize.Value; }
            set
            {
                _autoInitialize.Value = value;
                if (value == true) OnAutoInitialize();
            }
        }

#endregion

#region Methods

#if (WINRT)

        private TaskCompletionSource<bool> _initializationCoordinator = new TaskCompletionSource<bool>();
        
        public async Task EnsureIsInitialized()
        {
            await _initializationCoordinator.Task;
        }

        private async void OnAutoInitialize()
        {
            var _catalog = new ResourceCatalog();
            _resourceCatalog.Value = _catalog;
            await _catalog.Initialize();
            OnInitialized();
        }

#else

        private void OnAutoInitialize()
        {
            var _catalog = new ResourceCatalog();
            _resourceCatalog.Value = _catalog;
            _catalog.Initialize(OnInitialized);
        }

#endif

#endregion

    }
}

#endif